package com.ruoyi.file.utils;

import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class WatermarkUtils1 {

    public static File addTextWatermark(MultipartFile file, String watermarkText) throws IOException {
        BufferedImage srcImg = ImageIO.read(file.getInputStream());
        int width = srcImg.getWidth();
        int height = srcImg.getHeight();
        BufferedImage destImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics2D g2d = destImg.createGraphics();
        g2d.drawImage(srcImg, 0, 0, width, height, null);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

        // 设置水印文字
        Font font = new Font("微软雅黑", Font.BOLD, 20); // 调整字体大小
        g2d.setFont(font);
        g2d.setColor(Color.WHITE); // 水印颜色改为白色
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); // 设置透明度为50%

        // 将水印文本分成多行
        String[] lines = watermarkText.split("\\|");
        int lineHeight = g2d.getFontMetrics().getHeight();

        // 绘制顶部信息
        int yTop = 20; // 顶部间距
        for (int i = 0; i < 3; i++) {
            int x = 20; // 左边距
            g2d.setColor(Color.BLACK);
            g2d.drawString(lines[i], x + 1, yTop + 1);
            g2d.setColor(Color.WHITE);
            g2d.drawString(lines[i], x, yTop);
            yTop += lineHeight;
        }

        // 绘制底部信息
        int yBottom = height - 20 - lineHeight * (lines.length - 3); // 底部间距
        for (int i = 3; i < lines.length; i++) {
            int x = 20; // 左边距
            g2d.setColor(Color.BLACK);
            g2d.drawString(lines[i], x + 1, yBottom + 1);
            g2d.setColor(Color.WHITE);
            g2d.drawString(lines[i], x, yBottom);
            yBottom += lineHeight;
        }

        g2d.dispose();

        // 创建临时文件
        File watermarkFile = File.createTempFile("watermark_", ".jpg");
        ImageIO.write(destImg, "jpg", watermarkFile);
        return watermarkFile;
    }

    public static void main(String[] args) {
        try {
            // 读取本地文件并创建MultipartFile对象
            File file = new File("E:\\绿链恒生\\图片文件\\测试\\202412\\车辆\\新建文件夹\\1b8c2878b94931a6461d733c290d31a6_.jpg"); // 替换为实际图片路径
            FileInputStream input = new FileInputStream(file);
            MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "image/jpeg", input);

            // 添加水印
            String watermarkText = "596|集装箱号|45.00|元|费|收|45.00|kg|重|总|22360|kg|2025-01-14|13:25|星期重|16500|1装车称重照片|kg|重庆市渝北区金开大道98号再生资源有限公司6040.71283°N.74.00606°W";
            File watermarkedFile = addTextWatermark(multipartFile, watermarkText);

            // 保存水印后的文件
            File outputFile = new File("E:\\绿链恒生\\图片文件\\测试\\202412\\车辆\\新建文件夹\\watermarked_image.jpg"); // 替换为实际输出路径
            ImageIO.write(ImageIO.read(watermarkedFile), "jpg", outputFile);

            System.out.println("水印添加成功，文件保存到: " + outputFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}